<?php
function com_upload($a,$name) {
    // dump($name);exit();
    $upload = new \Think\Upload();
     // 实例化上传类
    $upload->maxSize = 3145728;
     // 设置附件上传大小
    $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
     // 设置附件上传类型
    $upload->rootPath = './Public/';
     // 设置附件上传根目录
    if($a=="1"){
        $upload->savePath  =      '/Uploads/Company/';
    }
    elseif ($a=="2") {
        $upload->savePath  =      '/Uploads/Shop/';
    }
    elseif ($a=="3") {
        $upload->savePath  =      '/Uploads/Goods/';
    }
    else{
        $upload->savePath  =      '/Uploads/Other/';
    }
    $upload->autoSub = true;
    $upload->subName ="$name";
     // 设置附件上传（子）目录
    $info = $upload->upload();
    // dump($upload->getError());exit();
    if (!$info) {
         // 上传错误提示错误信息
        // $this->error($upload->getError());
    }
    else {
         // 上传成功
        //$this->success('上传成功！');
        foreach ($info as $key =>$file) {
            $url[$key] = $file['savepath'] . $file['savename'];
        }
        //dump($url);exit();
        return $url;
    }

}

function returngoods($atod,$atsid,$sum){
    ini_set('soap.wsdl_cache_enabled','0');//关闭缓存
    $client=new \SoapClient('http://117.25.129.4:96/services/sms?wsdl');

    $arr = array(
            'in0'=>$atod,
            'in1'=>$atsid,
            'in2'=>$sum,
            'in3'=>md5($atod.$atsid.'CBS275021598872'.$sum),
        );

        $data = $client->returnGoods($arr)->out;

        return $data;

  }

  // 建组
function set_group($group_id){
    $appSec=C('INFO_APP_SEC');
    $nonce = mt_rand();
    $timeStamp = time();
    $signature = sha1($appSec.$nonce.$timeStamp);    //$appSec是平台分配
    $url = 'https://api.cn.ronghub.com/group/create.json';
    $postData ='userId=yyx001&groupId='.$group_id;
    $httpHeader = array(
    'App-Key:'.C('INFO_APP_KEY'),   //平台分配
    'Nonce:'.$nonce,        //随机数
    'Timestamp:'.$timeStamp,    //时间戳
    'Signature:'.$signature,         //签名
    'Content-Type: application/x-www-form-urlencoded',
    );
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    $result = curl_exec($ch);
    $ant_data=json_decode($result,true);
    curl_close($ch);
    return $ant_data["code"];
}
function getCateType($status){
    if ($status == 1) {
        return '填写';
    }
    if ($status == 2) {
        return '选择';
    }
}

function get_Type($status){
    if ($status == 0) {
        return '酒吧';
    }
    if ($status == 1) {
        return 'k歌';
    }
    if ($status == 2) {
        return '美食';
    }
}
function curlGet($url,$data){
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt ($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_HEADER, false);
    curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false);
    
    $temp = curl_exec($ch);
    curl_close($ch);
    return $temp;
}
//通过url，postdata，请求头获取信息
function urlGetJson($url,$postData,$httpHeader){
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    $yyx_result = curl_exec($ch);
    $yyx_data=json_decode($yyx_result,true);
    curl_close($ch);
    return $yyx_data;
}
//获取用户token
function get_rong_token($user_id){
    $result['id']= $user_id;
    $appSec= C('RONG_DEV_APP_SECRET');
    $nonce = mt_rand();
    $timeStamp = time();
    $signature = sha1($appSec.$nonce.$timeStamp);    //$appSec是平台分配
    $url = 'http://api.cn.ronghub.com/user/getToken.json';
    // $url = 'https://api.cn.rong.io/user/getToken.json';
    $postData = 'userId='.$result['id'];
    $httpHeader = array(
    'App-Key:'.C('RONG_DEV_APP_KEY'),   //平台分配
    'Nonce:'.$nonce,        //随机数
    'Timestamp:'.$timeStamp,    //时间戳
    'Signature:'.$signature,         //签名
    'Content-Type: application/x-www-form-urlencoded',
    );
    $yyx_data = urlGetJson($url,$postData,$httpHeader);
    $result['rong_id']=$yyx_data['token'];
    return $yyx_data['token'];
}

// 单挑消息
function system_to_one($user_id,$content,$extra){
    $wxopanid = M('user')->where(array('id'=>$user_id))->getField('openid');
    $status = get_rong_status($user_id);
    if ($status ==0) 
    {   
        $num = M('message')->where(array('shou_id'=>$user_id,'status'=>0))->count();
        $content = "您有".$num."条消息未读";
        $data = wx_message($wxopanid,urldecode($content));
    }
    $appSec = C('RONG_DEV_APP_SECRET');
    $nonce = mt_rand();
    $timeStamp = time();
    $signature = sha1($appSec.$nonce.$timeStamp);    //$appSec是平台分配
    $url = 'https://api.cn.ronghub.com/message/system/publish.json';
    $content='{"content":"'.$content.'","extra":"'.$extra.'"}';
    $content=urlencode($content);
    $postData ='fromUserId=System&toUserId='.$user_id.'&objectName=RC%3ATxtMsg&content='.$content;
    $httpHeader = array(
    'App-Key:'.C('RONG_DEV_APP_KEY'),   //平台分配
    'Nonce:'.$nonce,        //随机数
    'Timestamp:'.$timeStamp,    //时间戳
    'Signature:'.$signature,         //签名
    'Content-Type: application/x-www-form-urlencoded',
    );
    $datalist=urlGetJson($url,$postData,$httpHeader);
    
    return $datalist['code'];
}

// 检测有没有在线
function get_rong_status($user_id){
    $result['id']= $user_id;
    $appSec= C('RONG_DEV_APP_SECRET');
    $nonce = mt_rand();
    $timeStamp = time();
    $signature = sha1($appSec.$nonce.$timeStamp);    //$appSec是平台分配
    $url = 'http://api.cn.ronghub.com/user/checkOnline.json';
    // $url = 'https://api.cn.rong.io/user/getToken.json';
    $postData = 'userId='.$result['id'];
    $httpHeader = array(
    'App-Key:'.C('RONG_DEV_APP_KEY'),   //平台分配
    'Nonce:'.$nonce,        //随机数
    'Timestamp:'.$timeStamp,    //时间戳
    'Signature:'.$signature,         //签名
    'Content-Type: application/x-www-form-urlencoded',
    );
    $yyx_data = urlGetJson($url,$postData,$httpHeader);
    $result['status']=$yyx_data['status'];
    return $yyx_data['status'];
}
function getActiviStrStatus($activity){
    //如果爽约
    if($activity['status'] == 18){
        $str_status = '爽约';
    }elseif($activity['status'] == 16){//已经完成
        $str_status = '已完成';
    }elseif($activity['status'] == 1){
        $str_status = '已取消';
    }elseif($activity['status'] == 0){
        //有几种可能，分几个时间段
        //如果还没开始，则显示等待报名
        if($activity['start_time'] > time()){
            if($activity['join_ed'] > 0){
                $str_status = '待签到';
            }elseif($activity['bao_ed'] > 0){
                $str_status = '待确认用户';
            }else{
                $str_status = '等待报名';
            }
        }elseif(time() < $activity['start_time'] + 90*60){
            //活动9点开始，现在是10点半
            if($activity['is_arrive'] == 1){
                $str_status = '待完成';
            }elseif($activity['join_ed'] > 0){
                $str_status = '待签到';
            }else{
                $str_status = '已过期';
            }
        }else{
            if($activity['is_arrive'] == 1 ){
                $str_status = '待完成';
            }elseif($activity['join_ed'] > 0){
                $str_status = '爽约';
            }else{
                $str_status = '已过期';
            }
        }
    }
    return $str_status;
}

function getFeedStatus($status){
    if($status == 1){
        return '已处理';
    }else{
        return '未处理';
    }
}

    function gettoken(){
        //      if (session('wxtoken')) {
        //  return session('wxtoken');
        // }
        $appid=C('APP_ID');
        $secret=C('app_secret');
        $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $res=curlGet($url,$type="GET");
        $json=json_decode($res, true);
        session('wxtoken',$json['access_token']);
        return $json['access_token'];
    }

    // 给微信公众号发送模板消息 
    function wx_message($user_openid,$content){
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.gettoken();
        
        $data['touser']= $user_openid;//'oR_ADwGqA8nnBWwOvaClTJBqFGlo';//$user_openid;
        $data['template_id'] = 'o0ZttcurRDcCKJDXG5st0OaE0cC-ihhRi8myW_4jeQs';
        // $data['url'] = 'http://www.next.zm-idea.com/index.php/Home/Index/news.html';
        $data['url'] = 'http://www.next.zm-idea.com/index.php/Home/Index/index.html';
        $data['data'] = array(
            // 'content'=>$content
            'first'=>array(
                'value'=>'亲爱的用户，'.$content,
                "color"=>"#173177"
                ),
            'Good'=>array(
                'value'=>'个人信息',
                "color"=>"#173177"
                ),
            'contentType'=>array(
                'value'=>'待查看', //$content,
                "color"=>"#173177"
                ),
            'remark'=>array(
                'value'=>'点击查看详情！',
                "color"=>"#173177"
                ),
            );
        $data = ch_json_encode($data);
        $rurl = curlGet($url,$data);
        $rurl = json_decode($rurl,true);
        return $rurl;
        // dump($rurl);
    }
    // 解决 json_decode 不能中文
    function ch_json_encode($data) {

       $ret = ch_urlencode($data);
       $ret = json_encode($ret);
       return urldecode($ret);
    }
    function ch_urlencode($data) {
       if (is_array($data) || is_object($data)) {
           foreach ($data as $k => $v) {
               if (is_scalar($v)) {
                   if (is_array($data)) {
                       $data[$k] = urlencode($v);
                   } else if (is_object($data)) {
                       $data->$k = urlencode($v);
                   }
               } else if (is_array($data)) {
                   $data[$k] = ch_urlencode($v); //递归调用该函数
               } else if (is_object($data)) {
                   $data->$k = ch_urlencode($v);
               }
           }
       }
       return $data;
    }
?>
